package com.baidu.logindemos.ui.base

import android.os.Bundle
import android.view.MotionEvent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.viewbinding.ViewBinding
import com.baidu.logindemos.R
import com.baidu.logindemos.db.model.SharedPreferencesUtil
import com.baidu.logindemos.utils.KeyBoardUtil

/**
 * Activity的基类
 * 封装Activity公有的东西
 * 特殊的配置需要子类来完成
 */
abstract class BaseActivity<T:ViewBinding>: AppCompatActivity() {
    lateinit var mBinding: T
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        mBinding = initViewBinding()
        setContentView(mBinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        //初始化SharedPreference
        SharedPreferencesUtil.instance.init(this)
        KeyBoardUtil.instance.init(this)

        //初始化UI
        initUI()
    }

    open fun initUI(){

    }

    /**
     * 需要每个子类自己去创建自己的Binding类对象
     */
    abstract fun initViewBinding():T

    /**
     * 触摸事件
     */
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event?.action == MotionEvent.ACTION_DOWN) {
            KeyBoardUtil.instance.hide(mBinding.root)
        }
        return true
    }
}